//
//  DDActivenessViewController.m
//  DingDongZP
//
//  Created by 成才 向 on 16/4/7.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDActivenessViewController.h"
#import "DDUserInfoService.h"
#import "DDNewJobsViewController.h"

static NSString *const kSignUpDateKey = @"adffceasddf@34";

@interface DDActivenessViewController ()



@end

@implementation DDActivenessViewController

//- (void)loadView
//{
//    CGRect bounds = [UIScreen mainScreen].bounds;
//    UIView *view = [[UIView alloc] initWithFrame:bounds];
//    view.backgroundColor = [UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1];
//    self.view = view;
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"活跃度";
    
    self.cattleLab.text = @"看看新职位";
    self.avatarImageView.layer.cornerRadius = CGRectGetWidth(self.avatarImageView.frame)/2.f;
    self.avatarImageView.layer.masksToBounds = YES;
    
    [[DDUserInfoService sharedService] queryUserInfoWithFresh:NO callback:^(DDUserInfoModel *model) {
        NSString *url = [DDAPIGenerate imageURLWithPath:model.logo];
        [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"common_avatar"]];
    }];
    
    ///	活跃度从用户信息处取到
    self.activenessLab.text = self.userInfo.activeness;
    
    ///	被关注率
    self.attentionLab.text = [NSString stringWithFormat:@"活跃度越高，排名越靠前，越易被Boss关注\n\
        你的被关注度超过了%ld%%的牛人", self.userInfo.attention.integerValue];
    
    [self checkIsTimeToSignUp];
    
}
- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = true;
}

- (void)updateActiveness:(NSNumber *)activeness {
    self.userInfo.activeness = [NSString stringWithFormat:@"%@", activeness];
    self.activenessLab.text = self.userInfo.activeness;
}


#pragma mark - functions
- (void)checkIsTimeToSignUp
{
    ///	签到逻辑
    NSDate *lastSignTime = [[NSUserDefaults standardUserDefaults] objectForKey:kSignUpDateKey];
    if (lastSignTime && [lastSignTime timeIntervalSinceNow] < 60*60*24) {
        self.signUpBtn.selected = YES;
    }
    else {
        self.signUpBtn.selected = NO;
    }
}

- (void)setSignUpTime
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:kSignUpDateKey];
}

///	前往新职位界面
- (void)toNewJobsView
{
    DDNewJobsViewController *vc = [[DDNewJobsViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - events

/// 签到
- (IBAction)checkIn:(UIButton *)sender
{
    if (sender.selected) {
        return;
    }
    [self showLoading];
    __weak typeof(self) weakSelf = self;
    [[DDUserInfoService sharedService] addActivityWithType:kActivitySignIn callback:^(BOOL succ, id data) {
        [weakSelf hideLoading];
        if (succ) {
            [weakSelf showText:@"签到成功"];
            sender.selected = YES;
            [weakSelf setSignUpTime];
            [weakSelf updateActiveness:data];
        } else {
            if ([data isKindOfClass:[NSString class]]) {
                [weakSelf showText:data];
            }
        }
    }];
}

/// 沟通
- (IBAction)comunicateAction:(UIButton *)sender {
    [self toNewJobsView];
}

/// 看看新牛人
- (IBAction)lookoverAction:(UIButton *)sender {
    [self toNewJobsView];
}

/// 分享微主页
- (IBAction)shareAction:(UIButton *)sender {
    
    [self showLoading];
    __weak typeof(self) weakSelf = self;
    [[DDUserInfoService sharedService] addActivityWithType:kActivityShare callback:^(BOOL succ, id data) {
        [weakSelf hideLoading];
        if (succ) {
            [weakSelf updateActiveness:data];
        } else {
            if ([data isKindOfClass:[NSString class]]) {
                [weakSelf showText:data];
            }
        }
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
